<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    refresh_interVal: null, //接收父组件的刷新间隔值
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
      timer: null, //定义一个刷新的timer
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
    }
    this.chart = null;
  },
  methods: {

    initChart() {
      if (this.chart) {
        this.chart.dispose();
      }
      this.chart = echarts.init(this.$el, "macarons");
      this.chart.clear();
      this.setOptions(this.chartData);
    },
    setOptions(val) {
      var labelShow = {
        show: true,
        color: '#101010',
        fontSize: 15,
        formatter: [
          '{b| {b} }',
          '{d| {d}% }'
        ].join('\n'),
        rich: {
          b: {
            fontSize: 14,
            color: '#101010',
            padding: [0, 0, 10, 0]
          },
          d: {
            fontSize: 18,
            color: '#002F4F',
            fontWeight: 'bold',
            padding: [10, 0, 0, 0]
          },
        }
      };
      var seriesData = val.seriesData;
      let newArray = seriesData.map(obj => {
        return { ...obj, label: labelShow }; 
      });
     
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '5',
          data: val.legendData
        },
        color: ['#A3D2F6', '#FFBF6B'],
        series: [
          {
            name: '渗透率',
            type: 'pie',
            roseType: 'radius',
            radius: [0, 80],
            center: ['50%', '38%'],
            data: newArray,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
